package com.vitas.coin.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.google.gson.Gson;
import com.vitas.coin.data.bean.ChildrenVoiceBean;

import java.util.ArrayList;
import java.util.List;

public class PlayDao extends MyBaseDao {

    String TABLE_NAME = "play";

    @Override
    protected String getTableName() {
        return TABLE_NAME;
    }

    public boolean delete(ChildrenVoiceBean bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            int result = db.delete(getTableName(), "uid=?", new String[]{String.valueOf(bean.getId())});
            return result == 1;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return false;
    }

    public void delete(ChildrenVoiceBean bean, DBListener listener) {
        doInBackground(new Runnable() {

            @Override
            public void run() {
                boolean bo = delete(bean);
                if (listener != null) {
                    listener.onResult(bo, "", bean);
                }
            }
        });
    }

    private int getNum(ChildrenVoiceBean bean, SQLiteDatabase db) {
        Cursor cursor = null;
        try {
            String sql = "select num from play where uid='" + bean.getId() + "'";
            cursor = db.rawQuery(sql, null);
            cursor.moveToFirst();
            int rowCount = cursor.getInt(0);
            return rowCount;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            closeCursor(cursor);
        }
        return 0;
    }

    public void insert(ChildrenVoiceBean bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            if (db == null) {
                return;
            }
            int num = getNum(bean, db);
            if (num > 0) {
                update(bean, db, ++num);
            } else {
                ContentValues values = new ContentValues();
                values.put("uid", bean.getId());
                values.put("info", new Gson().toJson(bean));
                values.put("time", System.currentTimeMillis());
                values.put("num", 1);
                db.insert(TABLE_NAME, null, values);
            }
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
    }

    public void resetNum(ChildrenVoiceBean bean) {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            if (db == null) {
                return;
            }
            ContentValues values = new ContentValues();
            values.put("num", 1);
            db.update(TABLE_NAME, values, "uid=?", new String[]{bean.getId() + ""});
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
    }

    private boolean update(ChildrenVoiceBean bean, SQLiteDatabase db, int num) {
        try {
            ContentValues values = new ContentValues();
            values.put("time", System.currentTimeMillis());
            values.put("num", num);
            db.update(TABLE_NAME, values, "uid=?", new String[]{bean.getId() + ""});
            return true;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return false;
    }

    public ArrayList<ChildrenVoiceBean> getAll() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return new ArrayList<>();
            }
            cursor = db.rawQuery("select * from " + TABLE_NAME + " order by time desc", null);
            if (cursor == null || cursor.getCount() <= 0) {
                return new ArrayList<>();
            }
            ArrayList<ChildrenVoiceBean> list = new ArrayList<>();
            Gson gson = new Gson();
            while (cursor.moveToNext()) {
                String str = cursor.getString(cursor.getColumnIndexOrThrow("info"));
                ChildrenVoiceBean bean = gson.fromJson(str, ChildrenVoiceBean.class);
                list.add(bean);
            }
            return list;
        } catch (Throwable e) {
            return new ArrayList();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
    }

    public void getAll(DBListener listener) {
        doInBackground(new Runnable() {
            @Override
            public void run() {
                List<ChildrenVoiceBean> list = getAll();
                if (listener != null) {
                    listener.onResult(true, null, list);
                }
            }
        });
    }

    public long always() {
        SQLiteDatabase db = null;
        try {
            db = getDataBase();
            if (db == null) {
                return 0;
            }
            return always(db);
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            if (db != null) {
                closeDataBase();
            }
        }
        return 0;
    }

    public long always(SQLiteDatabase db) {
        Cursor cursor = null;
        try {
            String sql = "select count(*) from " + getTableName() + " where num>1 and time>" + (System.currentTimeMillis() - 24 * 3600 * 1000 * 20);
            cursor = db.rawQuery(sql, null);
            cursor.moveToFirst();
            long count = cursor.getLong(0);
            return count;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            closeCursor(cursor);
        }
        return 0;
    }

    public ArrayList<ChildrenVoiceBean> getAlways() {
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            db = getDataBase();
            if (db == null) {
                return new ArrayList<>();
            }
            String sql = "select * from " + TABLE_NAME + " where num>1 and time>" + (System.currentTimeMillis() - 24 * 3600 * 1000 * 20) +
                    " order by time desc";
            cursor = db.rawQuery(sql, null);
            if (cursor == null || cursor.getCount() <= 0) {
                return new ArrayList<>();
            }
            ArrayList<ChildrenVoiceBean> list = new ArrayList<>();
            Gson gson = new Gson();
            while (cursor.moveToNext()) {
                String str = cursor.getString(cursor.getColumnIndexOrThrow("info"));
                ChildrenVoiceBean bean = gson.fromJson(str, ChildrenVoiceBean.class);
                list.add(bean);
            }
            return list;
        } catch (Throwable e) {
            return new ArrayList();
        } finally {
            closeCursor(cursor);
            if (db != null) {
                closeDataBase();
            }
        }
    }

    public void getAlways(DBListener listener) {
        doInBackground(new Runnable() {
            @Override
            public void run() {
                List<ChildrenVoiceBean> list = getAlways();
                if (listener != null) {
                    listener.onResult(true, null, list);
                }
            }
        });
    }
}
